package com.zhoug.common3.utils;

import android.graphics.Color;

import java.io.UnsupportedEncodingException;
import java.util.Random;

/**
 * 随机字符生成器
 *
 * @Author 35574
 * @Date 2020/8/6
 * @Description
 */
public class RandomUtils {
    private static final String TAG = ">>>CharacterRandom";
    private static final boolean DEBUG = false;

    /**
     * 大小字母
     */
    public static final int FLAG_CHAR_UPPER = 1;//0001
    /**
     * 小写字母
     */
    public static final int FLAG_CHAR_LOWER = 2;//0010
    /**
     * 数字
     */
    public static final int FLAG_NUMBER = 4;//0100

    /**
     * 字符对应的编码
     */
    private static final int CHAR_A = 65;
    private static final int CHAR_Z = 90;
    private static final int CHAR_a = 97;
    private static final int CHAR_z = 122;
    private static final int CHAR_0 = 48;
    private static final int CHAR_9 = 57;


    /**
     * 生成随机汉字:去掉了很大一部分繁体字和生僻字
     * 4E00-9FA5
     *
     * @param length 汉字长度
     * @return
     */
    public static String randomChinese(int length) {
        if (length < 1) {
            length = 1;
        }
        StringBuilder str = new StringBuilder();//保存结果
        int highPos, lowPos;      //高位、低位
        Random random = new Random();
        byte[] b = new byte[2];      //转化为B类型
        for (int i = 0; i < length; i++) {
            highPos = 176 + Math.abs(random.nextInt(39)); //计算高位数
            lowPos = 161 + Math.abs(random.nextInt(93));//计算低位数
            b[0] = (Integer.valueOf(highPos)).byteValue();  //高字节
            b[1] = (Integer.valueOf(lowPos)).byteValue();//低字节
            try {
                str.append(new String(b, "GBK"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }

        return str.toString();
    }

    /**
     * 随机生成指定长度的数字字符串
     *
     * @param length
     * @return
     */
    public static String randomNumber(int length) {
        Random random = new Random();
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < length; i++) {
            stringBuilder.append(random.nextInt(10));//0-9
        }
        return stringBuilder.toString();
    }

    /**
     * 随机生成指定长度的字符串:大写A-Z
     *
     * @param length
     * @return
     */
    public static String randomA_Z(int length) {
        Random random = new Random();
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < length; i++) {
            stringBuilder.append((char) (CHAR_A + random.nextInt(26)));//0-25
        }
        return stringBuilder.toString();
    }


    /**
     * 随机生成指定长度的字符串:a-z,A-Z,0-9混合字符串
     *
     * @param length
     * @param flags  字符串类型:大小写字母,数字
     *               {@link #FLAG_CHAR_UPPER}
     *               {@link #FLAG_CHAR_LOWER}
     *               {@link #FLAG_NUMBER}
     * @param others 其它需要添加的字符 eg:{"_","-",".",...}
     * @return
     */
    public static String randomCharacter(int length, int[] flags, String[] others) {
        Random random = new Random();
        StringBuilder stringBuilder = new StringBuilder();
        //默认全部类型
        if (flags == null || flags.length == 0) {
            flags = new int[]{FLAG_CHAR_UPPER, FLAG_CHAR_LOWER, FLAG_NUMBER};
        }
        int max = flags.length;
        if (others != null && others.length > 0) {
            max++;
        }
        int flag;
        for (int i = 0; i < length; i++) {
            //随机取出一个类型
            int index = random.nextInt(max);
            if (index < flags.length) {
                flag = flags[index];
                switch (flag) {
                    case FLAG_CHAR_UPPER://大小字母
                        stringBuilder.append((char) (CHAR_A + random.nextInt(26)));
                        break;
                    case FLAG_CHAR_LOWER://小写字母
                        stringBuilder.append((char) (CHAR_a + random.nextInt(26)));
                        break;
                    case FLAG_NUMBER://数字
                        stringBuilder.append(random.nextInt(10));
                        break;

                }
            } else {
                stringBuilder.append(others[random.nextInt(others.length)]);
            }
        }

        return stringBuilder.toString();
    }

    /**
     * 随机生成指定长度的字符串:a-z,A-Z,0-9混合字符串
     *
     * @param length
     * @param flags  字符串类型:大小写字母,数字
     *               {@link #FLAG_CHAR_UPPER}
     *               {@link #FLAG_CHAR_LOWER}
     *               {@link #FLAG_NUMBER}
     * @return
     */
    public static String randomCharacter(int length, int[] flags) {
        Random random = new Random();
        StringBuilder stringBuilder = new StringBuilder();
        //默认全部类型
        if (flags == null || flags.length == 0) {
            flags = new int[]{FLAG_CHAR_UPPER, FLAG_CHAR_LOWER, FLAG_NUMBER};
        }
        int fLen = flags.length;
        int flag;
        for (int i = 0; i < length; i++) {
            //随机取出一个类型
            flag = flags[random.nextInt(fLen)];
            switch (flag) {
                case FLAG_CHAR_UPPER://大小字母
                    stringBuilder.append((char) (CHAR_A + random.nextInt(26)));
                    break;
                case FLAG_CHAR_LOWER://小写字母
                    stringBuilder.append((char) (CHAR_a + random.nextInt(26)));
                    break;
                case FLAG_NUMBER://数字
                    stringBuilder.append(random.nextInt(10));
                    break;
            }
        }
        return stringBuilder.toString();
    }

    /**
     * 随机生成指定长度的字符串:a-z,A-Z,0-9混合字符串
     *
     * @param length
     * @return
     * @see #randomCharacter(int, int[])
     */
    public static String randomCharacter(int length) {
        return randomCharacter(length, new int[]{FLAG_CHAR_UPPER, FLAG_CHAR_LOWER, FLAG_NUMBER});
    }

    /**
     * 随机生成颜色
     * @return
     */
    private int randomColor() {
        StringBuilder stringBuilder = new StringBuilder();
        Random random = new Random();
        String haxString;
        for (int i = 0; i < 3; i++) {
            haxString = Integer.toHexString(random.nextInt(0xFF));
            if (haxString.length() == 1) {
                haxString = "0" + haxString;
            }
            stringBuilder.append(haxString);
        }
        return Color.parseColor("#" + stringBuilder.toString());
    }

}